package cc.core.ccequalizer

import android.content.Context
import android.content.SharedPreferences
import android.media.audiofx.BassBoost
import android.media.audiofx.Equalizer
import android.telephony.CellSignalStrength
import android.util.Log
import java.lang.Exception

private const val TAG = "EqHolder"
const val CUSTOMER_PRESET: Short = -1

object EqHolder {
    var equalizer: Equalizer? = null
    var bassBoost: BassBoost? = null
    lateinit var sp: SharedPreferences
    const val KEY_EQ_SETTINGS = "eq_settings"
    const val KEY_EQ_ENABLE = "eq_enable"
    const val KEY_BASS_BOOST_ENABLE = "key_bass_boost_enable"
    const val KEY_BASS_BOOST_SETTINGS = "key_bass_boost_settings"
    const val KEY_PRESET = "key_preset"
    fun init(audioSessionId: Int, context: Context) {
        initEq(audioSessionId, context)
        initBass(audioSessionId, context)
    }

    private fun initBass(audioSessionId: Int, context: Context) {
        bassBoost = try {
            BassBoost(0, audioSessionId)
        } catch (e: Exception) {
            null
        }
        bassBoost?.enabled = true
    }

    private fun initEq(audioSessionId: Int, context: Context) {
        sp = context.getSharedPreferences("eq", Context.MODE_PRIVATE)
        val savedEq = sp.getString(KEY_EQ_SETTINGS, "")

        equalizer = try {
            Equalizer(0, audioSessionId)
        } catch (e: Exception) {
            Log.e(TAG, e.toString())
            null
        }
        currentPreset = sp.getInt(KEY_PRESET, CUSTOMER_PRESET.toInt()).toShort()
        if (currentPreset != CUSTOMER_PRESET) {
            setPreset(currentPreset)
        } else
            if (savedEq?.isNotBlank() == true) {
                equalizer?.let {
                    it.properties = Equalizer.Settings(savedEq)
                }
            }
        equalizer?.setEnabled(sp.getBoolean(KEY_EQ_ENABLE, false))
    }

    fun saveEq() {
        equalizer?.let {
            val setting = it.properties
            sp.edit().putString(KEY_EQ_SETTINGS, setting.toString())
                .putBoolean(KEY_EQ_ENABLE, it.enabled).apply()
        }

        bassBoost?.let {
            sp.edit().putBoolean(KEY_BASS_BOOST_ENABLE, isBassEnable)
                .putString(KEY_BASS_BOOST_SETTINGS, it.properties.toString()).apply()
        }
        sp.edit().putInt(KEY_PRESET, currentPreset.toInt()).apply()
    }

    private var bassBoostStrength: Short = 0
    private var isBassEnable = false
    fun setBassBoostEnable(isEnable: Boolean) {
        isBassEnable = isEnable
        bassBoost?.setStrength(if (isEnable) bassBoostStrength else 0)
    }

    fun setBassBoostStrength(strength: Short) {
        bassBoostStrength = strength
        bassBoost?.setStrength(strength)
    }

    fun isEqEnable(): Boolean {
        return if (equalizer != null) equalizer!!.enabled else false
    }

    fun isBassBoostEnable(): Boolean {
        return bassBoost != null && isBassEnable
    }

    fun isSupportBassBoost(): Boolean {
        return if (bassBoost != null) bassBoost!!.strengthSupported else false
    }

    fun getYuShe(): MutableList<Pair<String, Short>> {
        val list = mutableListOf<Pair<String, Short>>()
        val cnMap = mutableMapOf<String, String>()
        cnMap.put("Normal", "正常")
        cnMap.put("Classical", "古典")
        cnMap.put("Dance", "舞曲")
        cnMap.put("Flat", "扁平")
        cnMap.put("Folk", "民谣")
        cnMap.put("Heavy Metal", "重金属")
        cnMap.put("Hip Hop", "嘻哈")
        cnMap.put("Jazz", "爵士")
        cnMap.put("Pop", "流行")
        cnMap.put("Rock", "摇滚")
        equalizer?.let {
            for (i in 0 until it.numberOfPresets) {
                val nameEn = it.getPresetName(i.toShort())
                val nameCn = cnMap[nameEn] ?: nameEn
                list.add(Pair<String, Short>(nameCn, i.toShort()))
            }
        }
        return list
    }

    fun setBandLevel(brand: Short, value: Short) {
        equalizer?.setBandLevel(brand, value)
        currentPreset = CUSTOMER_PRESET
    }

    /**
     * 设置预设
     */
    fun setPreset(preset: Short) {
        currentPreset = preset
        if (currentPreset != CUSTOMER_PRESET) {
            equalizer?.usePreset(preset)
        }
        presetChangedListener?.changed()
    }

    private var currentPreset: Short = CUSTOMER_PRESET
    /**
     * 获取当前预设
     */
    fun getCurPreset(): Short {
        return currentPreset
    }

    var presetChangedListener: PresetChangedListener? = null
}

interface PresetChangedListener {
    fun changed()
}